<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { queryOverviewApi } from '@/api/home'

const router = useRouter()

// 页面加载完毕后, 执行查询
onMounted(() => {
  loadOverviewData()
})

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 加载概览数据
const loadOverviewData = async () => {
  try {
    const result = await queryOverviewApi()
    if(result.code){
      overviewData.value = result.data
    }
  } catch (error) {
    console.error('加载概览数据失败:', error)
  }
}

// 快捷操作 - 跳转到对应页面
const goToPage = (path) => {
  router.push(path)
}
</script>

<template>
  <div class="home">
    <!-- 欢迎标题 -->
    <div class="welcome-title">
      <h1>欢迎您, 进入轻客管家系统</h1>
    </div>

    <!-- 概览卡片区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <div class="card-header">
          <h2>线索概览</h2>
        </div>
        <div class="card-content">
          <div class="data-grid">
            <div class="data-item">
              <div class="data-label">总线索数</div>
              <div class="data-value">{{ overviewData.clueTotal }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">待分配线索数</div>
              <div class="data-value">{{ overviewData.clueWaitAllot }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">待跟进线索数</div>
              <div class="data-value">{{ overviewData.clueWaitFollow }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">跟进中线索数</div>
              <div class="data-value">{{ overviewData.clueFollowing }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">伪线索数</div>
              <div class="data-value">{{ overviewData.clueFalse }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">转商机线索数</div>
              <div class="data-value">{{ overviewData.clueConvertBusiness }}</div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 商机概览 -->
      <div class="overview-card">
        <div class="card-header">
          <h2>商机概览</h2>
        </div>
        <div class="card-content">
          <div class="data-grid">
            <div class="data-item">
              <div class="data-label">总商机数</div>
              <div class="data-value">{{ overviewData.businessTotal }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">待分配商机数</div>
              <div class="data-value">{{ overviewData.businessWaitAllot }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">待跟进商机数</div>
              <div class="data-value">{{ overviewData.businessWaitFollow }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">跟进中商机数</div>
              <div class="data-value">{{ overviewData.businessFollowing }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">回收商机数</div>
              <div class="data-value">{{ overviewData.businessFalse }}</div>
            </div>
            <div class="data-item">
              <div class="data-label">转客户商机数</div>
              <div class="data-value">{{ overviewData.businessConvertCustomer }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions">
      <div class="action-item" @click="goToPage('/resource/course')">
        <div class="action-icon">
          <el-icon><Reading /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">课程管理</div>
          <div class="action-subtitle">课程信息维护</div>
        </div>
      </div>

      <div class="action-item" @click="goToPage('/resource/activity')">
        <div class="action-icon">
          <el-icon><Calendar /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">活动管理</div>
          <div class="action-subtitle">营销活动管理</div>
        </div>
      </div>

      <div class="action-item" @click="goToPage('/clue/list')">
        <div class="action-icon">
          <el-icon><Promotion /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">线索管理</div>
          <div class="action-subtitle">潜在客户追踪</div>
        </div>
      </div>

      <div class="action-item" @click="goToPage('/business/list')">
        <div class="action-icon">
          <el-icon><Opportunity /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">商机管理</div>
          <div class="action-subtitle">销售机会管理</div>
        </div>
      </div>

      <div class="action-item" @click="goToPage('/customer')">
        <div class="action-icon">
          <el-icon><User /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">客户管理</div>
          <div class="action-subtitle">客户信息维护</div>
        </div>
      </div>

      <div class="action-item" @click="goToPage('/system/log')">
        <div class="action-icon">
          <el-icon><Document /></el-icon>
        </div>
        <div class="action-content">
          <div class="action-title">日志统计</div>
          <div class="action-subtitle">系统操作日志</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100%;
}

.welcome-title {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-title h1 {
  font-size: 50px;
  color: #333;
  margin: 0;
  font-weight: 500;
  font-family: 楷体, KaiTi, serif;
}

.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-card {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.card-header {
  background: linear-gradient(90deg, #1890ff 0%, #36cfc9 100%);
  color: #fff;
  padding: 15px 20px;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.card-content {
  padding: 20px;
}

.data-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.data-item {
  background-color: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 6px;
  padding: 15px;
  text-align: center;
  transition: all 0.3s ease;
}

.data-item:hover {
  border-color: #1890ff;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
}

.data-label {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.data-value {
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.quick-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.action-item {
  background-color: #fff;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 25px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.action-item:hover {
  border-color: #1890ff;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.2);
  transform: translateY(-2px);
}

.action-icon {
  margin-bottom: 15px;
}

.action-icon .el-icon {
  font-size: 36px;
  color: #1890ff;
}

.action-title {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}

.action-subtitle {
  font-size: 14px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }
  
  .quick-actions {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .data-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .quick-actions {
    grid-template-columns: 1fr;
  }
  
  .welcome-title h1 {
    font-size: 24px;
  }
}
</style>
